//
//  UIBarButtonItem+Extension.swift
//  XJWeiBo
//
//  Created by 李旭杰 on 2017/3/26.
//  Copyright © 2017年 李旭杰. All rights reserved.
//

import UIKit

extension UIBarButtonItem {
    /// 遍历构造函数
    convenience init(title: String, imageName: String? = nil, target: Any, action: Selector) {
        self.init()
        /// 创建 button
        let button = UIButton()
        /// 设置文字
        button.setTitle(title, for: .normal)
        
        /// 设置不同状态下的字体颜色
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        
        /// 设置字号
        button.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        
        /// 设置图片
        if imageName != nil {
            button.setImage(UIImage(named: imageName!), for: .normal)
        }
        
        /// 大小匹配
        button.sizeToFit()
        
        /// 监听摁扭的点击事件
        button.addTarget(target, action: action, for: .touchUpInside)
        
        /// 关联自定义 view 
        customView = button
    }
}
